package src.cs2340.todoApp;
import java.util.ArrayList;

public class Database{
	private final static Database instance = new Database();
	/**
	 * ArrayList holding the account information
	 * @throws IOException 
	 */
	private ArrayList<Account> accounts;
	
	private Database(){
		accounts = new ArrayList<Account>();
	/*	
		File f = WelcomeScreen.dbFile;
		FileReader fRead = null;
		String line = null;
		try {
			fRead = new FileReader(f);
			LineNumberReader lineReader = new LineNumberReader(fRead);
			line = lineReader.readLine();
			while(line != null){
				String[] strArr = line.split(" ");
				addAccount(new Account(strArr[0],strArr[1],strArr[2],strArr[3], Database.getInstance()));
				line = lineReader.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AddAccountException ex){
			//TODO
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	*/		
	}
	
	/**
	 * Returns the current instance in use by app
	 * @return current instance
	 */
	public static Database getInstance(){
		return instance;
	}
		
	/**
	 * AddAccountException tracks whatever problems might occur when an account is created.
	 * Add an account to the database 
	 * This method will need to be changed when we stop using an array list.
	 * @param acc
	 * @throws AddAccountException
	 */
	public void addAccount(Account acc) throws AddAccountException{
		if(dbContains(acc)){
			throw new AddAccountException("Already Created");
		}
		else{
			accounts.add(acc);
		}
	}
	
	/**
	 * Checks if the account that's passed in is in the array.
	 * @param acc
	 * @return
	 */
	protected boolean login(Account acc){
		return dbContains(acc);
	}
	/**
	 * 
	 * @param acc
	 * @return
	 */
	private boolean dbContains(Account acc) {
		for(int i=0; i<accounts.size(); i++){
			if(accounts.get(i).equals(acc)){
				return true;
			}
		}
		return false;
	}
	/**
	 *  Pass in an account to update that account's entry to what is passed in.
	 *  Equality of accounts is based on their username/password.
	 * @param acc
	 * @return
	 */
	protected void update(Account acc){
		for(int i=0; i<accounts.size(); i++){
			if(accounts.get(i).equals(acc)){
				accounts.set(i, acc);
			}
		}
	}
	/**
	 * Pass in an instance of an account to get the version that the database currently holds
	 * @param acc
	 * @return
	 */
	protected Account getUpdate(Account acc){
		for(int i=0; i<accounts.size(); i++){
			if(accounts.get(i).equals(acc)){
				return accounts.get(i);
			}
		}
		return null;
	}
//	
//	public int getNumAccounts() {
//		return accounts.size();
//	}
}
